<?php

/*
 *	Framework namespace
 */
namespace Nano
{
	/*
	 *	Headers controll class
	 */
	class Headers
	{
		/*
		 *	$headers	array	Array with headers to send
		 *
		 *	@static
		 */
		static public $headers = array();
		
		/*
		 *	$http	string	Default protocol version
		 *
		 *	@static
		 */
		static public $http = 'HTTP/1.1';
		
		/*
		 *	$data	array	Headers code and other data
		 *
		 *	@static
		 */
		static public $list = array
		(
			// Informational
			100 => 'Continue',
			101 => 'Switching Protocols',
			102 => 'Processing',
			
			// Success
			200 => 'OK',
			201 => 'Created',
			202 => 'Accepted',
			203 => 'Non-Authoritative Information',
			204 => 'No Content',
			205 => 'Reset Content',
			206 => 'Partial Content',
			207 => 'Multi-Status',
			226 => 'IM Used',
			
			// Redirection
			300 => 'Multiple Choices',
			301 => 'Moved Permanently',
			302 => 'Found',
			303 => 'See Other',
			304 => 'Not Modified',
			305 => 'Use Proxy',
			306 => 'Switch Proxy',
			307 => 'Temporary Redirect',
			
			// Client Error
			400 => 'Bad Request',
			401 => 'Unauthorized',
			402 => 'Payment Required',
			403 => 'Forbidden',
			404 => 'Not Found',
			405 => 'Method Not Allowed',
			406 => 'Not Acceptable',
			407 => 'Proxy Authentication Required',
			408 => 'Request Timeout',
			409 => 'Conflict',
			410 => 'Gone',
			411 => 'Length Required',
			412 => 'Precondition Failed',
			413 => 'Request Entity Too Large',
			414 => 'Request-URI Too Long',
			415 => 'Unsupported Media Type',
			416 => 'Requested Range Not Satisfiable',
			417 => 'Expectation Failed',
			418 => 'I\'m a teapot',
			422 => 'Unprocessable Entity',
			423 => 'Locked',
			424 => 'Failed Dependency',
			425 => 'Unordered Collection',
			426 => 'Upgrade Required',
			449 => 'Retry With',
			456 => 'Unrecoverable Error',
			
			// Server Error
			500 => 'Internal Server Error',
			501 => 'Not Implemented',
			502 => 'Bad Gateway',
			503 => 'Service Unavailable',
			504 => 'Gateway Timeout',
			505 => 'HTTP Version Not Supported',
			506 => 'Variant Also Negotiates',
			507 => 'Insufficient Storage',
			509 => 'Bandwidth Limit Exceeded',
			510 => 'Not Extended'
		);
		
		/*
		 *	Clear default headers
		 *
		 *	@static
		 */
		static public function clear()
		{
			self::$headers = headers_list();
			
			foreach(self::$headers as $num => $header)
			{
				$header = explode(':', $header);
	
				header_remove($header[0]);
					
				unset(self::$headers[$num]);
			}
		}
		
		/*
		 *	Add header
		 *
		 *	@param	string	Header data
		 *	@param	string	Header value [optional]
		 *
		 *	@static
		 */
		static public function add($header)
		{
			return self::$headers[] = $header; 
		}
		
		/*
		 *	Send header
		 *
		 *	@param	string	Header
		 *
		 *	@static
		 */
		static public function send($header)
		{
			if(is_int($header) && key_exists($header, self::$list))
			{
				return self::$headers[] = self::$http.' '.$header.' '.self::$list[$header];
			}
			
			header($header);
		}
		
		/*
		 *	Remove header
		 *
		 *	@param	string	Header name
		 *	@return	boolean
		 */
		static public function remove($header)
		{
			return (boolean)header_remove($header);
		}
		
		/*
		 *	Send headers
		 */
		static public function flush()
		{
			if(self::sent())
			{
				throw new Exception(__CLASS__.': Headers already sent');
			}
			
			foreach(self::$headers as $num => $header)
			{
				self::send($header);
			}
		}
		
		/*
		 *	Is headers already sended?
		 *
		 *	@return	boolean
		 */
		static public function sent()
		{
			return (boolean)headers_sent();
		}
	}
}